package com.woter.fact.async.util;

import java.lang.reflect.Method;
import java.util.regex.Pattern;

import org.springframework.aop.support.AopUtils;
import org.springframework.util.ClassUtils;

/**
 * @author woter
 * @date 2016-4-5 下午5:31:28
 * @version
 */
public class CommonUtil {

	/**
	 * @description:获取代理类的父类, CGLib
	 */
	public static Class<?> getClass(Object object) {
		boolean isCglibProxy = false;
		if (AopUtils.isCglibProxy(object)) {
			isCglibProxy = true;
		}
		if (isCglibProxy || ClassUtils.isCglibProxy(object)) {
			isCglibProxy = true;
		}
		Class<?> targetClass = object.getClass();
		if (isCglibProxy) {
			targetClass = targetClass.getSuperclass();
		}
		return targetClass;
	}

	/**
	 * @description: 构造缓存的 key
	 * 缓存的 key 是 className + methodName + #param ...
	 */
	public static String buildkey(Object object, Method method) {
		ValidationUtils.checkNotNull(object);
		return new StringBuilder(getClass(object).getName()).append(".")
				.append(buildMethod(method)).toString();
	}

	public static String buildMethod(Method method) {
		StringBuilder strbuilder = new StringBuilder(method.getName());
		Class<?>[] parameterTypes = method.getParameterTypes();
		if (parameterTypes != null && parameterTypes.length > 0) {
			for (Class<?> parameterType : parameterTypes) {
				strbuilder.append("#").append(parameterType.getName());
			}
		}
		return strbuilder.toString();
	}

	/**
	 * @description:判断是否为数组
	 */
	public static boolean isNumber(String number) {
		String regex = "^[0-9]+$";
		return Pattern.matches(regex, number);
	}
}
